package com.hrzyit.whistle.user.utils

import com.hrzyit.base.common.BaseConstant
import com.hrzyit.base.utils.AppPrefsUtils
import com.hrzyit.provider.common.ProviderConstant
import com.hrzyit.whistle.user.data.bean.UserInfo
import com.hrzyit.base.utils.digest.AESUtils
import java.util.*

/**
 * 本地存储用户信息
 * @author YangWei
 * @date 2019/3/27
 */
object UserPrefsUtils {

    /**
     * 登录记录我功能
     */
    fun putRememberMeInfo(
        isRememberMe: Boolean,
        pwd: String,
        account: String,
        mobile: String,
        secretKey: ByteArray
    ) {
        // user_token
        when (isRememberMe) {
            true -> {
                AppPrefsUtils.putBoolean(ProviderConstant.R_IS_REMEMBER_ME, isRememberMe)
                AppPrefsUtils.putString(ProviderConstant.R_KEY_PWD, AESUtils.encrypt(pwd, secretKey))
                AppPrefsUtils.putString(ProviderConstant.R_KEY_ACCOUNT, AESUtils.encrypt(account, secretKey))
                AppPrefsUtils.putString(ProviderConstant.R_KEY_MOBILE, AESUtils.encrypt(mobile, secretKey))
            }
            false -> {
                AppPrefsUtils.putBoolean(ProviderConstant.R_IS_REMEMBER_ME, false)
                AppPrefsUtils.remove(ProviderConstant.R_KEY_PWD)
                AppPrefsUtils.remove(ProviderConstant.R_KEY_ACCOUNT)
                AppPrefsUtils.remove(ProviderConstant.R_KEY_MOBILE)
            }
        }

    }

    /**
     * 获取记录我功能数据
     */
    fun getRememberMeInfo(secretKey: ByteArray): Map<String, Any> {

        val loginUserInfo = mutableMapOf<String, Any>()

        when (val isRememberMe = AppPrefsUtils.getBoolean(ProviderConstant.R_IS_REMEMBER_ME)) {
            true -> {
                loginUserInfo[ProviderConstant.R_IS_REMEMBER_ME] = isRememberMe

                var pwd = AppPrefsUtils.getString(ProviderConstant.R_KEY_PWD)

                pwd = AESUtils.decrypt(pwd, secretKey)

                loginUserInfo[ProviderConstant.R_KEY_PWD] = pwd

                var account = AppPrefsUtils.getString(ProviderConstant.R_KEY_ACCOUNT)

                account = AESUtils.decrypt(account, secretKey)

                loginUserInfo[ProviderConstant.R_KEY_ACCOUNT] = account

                var mobile = AppPrefsUtils.getString(ProviderConstant.R_KEY_MOBILE)

                mobile = AESUtils.decrypt(mobile, secretKey)

                loginUserInfo[ProviderConstant.R_KEY_MOBILE] = mobile

            }
        }
        return loginUserInfo
    }


    /**
     * 退出登录时，传入null,清空存储
     */
    fun putUserInfo(userInfo: UserInfo?) {
        AppPrefsUtils.putString(
            BaseConstant.KEY_SP_TOKEN, userInfo?.user_token ?: ""
        )
        // user_token
        AppPrefsUtils.putLong(
            BaseConstant.KEY_SP_USER_TOKEN, userInfo?.user?.id ?: 0L
        )
        AppPrefsUtils.putString(
            ProviderConstant.KEY_SP_USER_AVATAR, userInfo?.user?.avatar ?: ""
        )

        AppPrefsUtils.putString(
            ProviderConstant.KEY_SP_USER_ACCOUNT, userInfo?.user?.account ?: ""
        )
        AppPrefsUtils.putString(
            ProviderConstant.KEY_SP_USER_NAME, userInfo?.user?.name ?: ""
        )
        AppPrefsUtils.putString(
            ProviderConstant.KEY_SP_USER_MOBILE, userInfo?.user?.mobile ?: ""
        )

        AppPrefsUtils.putString(
            ProviderConstant.KEY_SP_USER_PHONE, userInfo?.user?.phone ?: ""
        )

        AppPrefsUtils.putInt(
            ProviderConstant.KEY_SP_USER_SEX, userInfo?.user?.sex ?: 1
        )
        AppPrefsUtils.putLong(
            ProviderConstant.KEY_SP_REPORT_DEPT_ID, userInfo?.user?.reportDeptId ?: 0L)

        AppPrefsUtils.putString(
            ProviderConstant.KEY_SP_REPORT_DEPT_NAME, userInfo?.user?.reportDeptName ?: "请选择上报单位"
        )

        AppPrefsUtils.putLong(
            ProviderConstant.KEY_SP_USER_ROLE_ID, userInfo?.user?.roleId ?: 0
        )
        AppPrefsUtils.putLong(
            ProviderConstant.KEY_SP_USER_DEPT_ID, userInfo?.user?.deptId ?: 0
        )
    }
    /**
     * 退出登录时,清空存储
     */
    fun clearInfo() {
        AppPrefsUtils.remove(BaseConstant.KEY_SP_TOKEN)
        AppPrefsUtils.remove(BaseConstant.KEY_SP_USER_TOKEN)
        AppPrefsUtils.remove(ProviderConstant.KEY_SP_USER_AVATAR)
        AppPrefsUtils.remove(ProviderConstant.KEY_SP_USER_ACCOUNT)
        AppPrefsUtils.remove(ProviderConstant.KEY_SP_USER_NAME)
        AppPrefsUtils.remove(ProviderConstant.KEY_SP_USER_MOBILE)
        AppPrefsUtils.remove(ProviderConstant.KEY_SP_USER_PHONE)
        AppPrefsUtils.remove(ProviderConstant.KEY_SP_USER_SEX)
        AppPrefsUtils.remove(ProviderConstant.KEY_SP_REPORT_DEPT_ID)
        AppPrefsUtils.remove(ProviderConstant.KEY_SP_REPORT_DEPT_NAME)
        AppPrefsUtils.remove(ProviderConstant.KEY_SP_USER_ROLE_ID)
        AppPrefsUtils.remove(ProviderConstant.KEY_SP_USER_DEPT_ID)
    }
}